# 类中的私有变量是以“__“开头的变量，私有变量不能直接访问
# 注意：以”__“开头并且以”__“结尾的变量叫做特殊变量，可以直接访问
class Student(object):
    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def get_name(self):
        return self.__name

    def get_score(self):
        return self.__score
    def set_score(self, score):
        if 0 <= score <= 100:
            self.__score = score
        else:
            raise ValueError('bad score')

bart = Student('Bart Simpson', 59)
print(bart.get_name())

# 注意：不能通过以下方式直接对类的私有变量进行赋值，否则这将创建一个新的变量，而不是修改原有的私有变量
bart.__name = "New name"
print(bart.__name)
print(bart.get_name())

# experiment
class Student(object):
    def __init__(self, name, gender):
        self.name = name
        self.__gender = gender
    def get_gender(self):
        return self.__gender
    def set_gender(self, gender):
        if gender != 'male' and gender != 'female':
            raise ValueError('bad gender')
        self.__gender = gender

# 测试:
bart = Student('Bart', 'male')
if bart.get_gender() != 'male':
    print('测试失败!')
else:
    bart.set_gender('female')
    if bart.get_gender() != 'female':
        print('测试失败!')
    else:
        print('测试成功!')
print(bart.__gender)